<!--
 * @Description: 教师业绩模块-专著与主编教材
 * @Version: 1.0
 * @Autor: tao
 * @Date: 2021-01-15 16:19:27
 * @LastEditors: tao
 * @LastEditTime: 2021-04-25 13:20:54
-->

<template>
  <div class="Inventory">
    <Management>
      <Top
        slot="top"
        @showModel="showModel"
        @showAll="showAll"
        @search="onSearch"
        :add="true"
      ></Top>
      <Table
        slot="body"
        :title="columns"
        :tableData="tableData"
        :total="total"
        :current="current"
        @edit="edit"
        @del="del"
        other-txt="审核"
        @other="other"
        @changePage="changePage"
      ></Table>
    </Management>
    <Dialog
      v-if="isShow"
      :show="isShow"
      :row="row"
      @confirm="confirm"
      @cancel="cancel"
    >
    </Dialog>

    <Card
      :list="this.row"
      :show="showing"
      @confirm="CardConfirm"
      @cancel="cancel"
      @noPass="CardCancel"
    >
      <div name="card">ss</div>
    </Card>
  </div>
</template>

<script>
//组件
import Table from "components/common/table/Table";
import Management from "components/common/management/Management";
import Top from "./component/Top";
import Dialog from "./component/Dialog";
import Card from "@/views/base/b-base/component/Card";
//网络请求
import {
  getDataByPageAndSize,
  appendData,
  deleteById,
  editData,
  LikeSearch,
  Check,
} from "@/network/Base/b-base";
import { getSystem } from "@/network/Info/BaseInfo";

export default {
  //注册组件
  components: {
    Card,
    Table,
    Top,
    Management,
    Dialog,
  },
  data() {
    const columns = [
      {
        title: "id",
        dataIndex: "id",
      },
      {
        title: "企业名称",
        dataIndex: "name",
      },
      {
        title: "营业执照编号",
        dataIndex: "businessId",
      },
      {
        title: "企业联系人",
        dataIndex: "leader",
      },
      {
        title: "手机号",
        dataIndex: "phone",
      },
      {
        title: "email",
        dataIndex: "email",
      },
      {
        title: "所属教研室",
        dataIndex: "belong",
      },
      {
        title: "状态",
        dataIndex: "status",
      },
      {
        title: "备注",
        dataIndex: "text",
      },
      {
        title: "附件文件",
        dataIndex: "file",
        width: 150,
      },
      {
        title: "审核结果",
        dataIndex: "check",
      },
    ];
    //表头 及 对应的 数据字段名
    return {
      tableData: [],
      columns,
      isShow: false,
      checking: false,
      total: 0,
      showing: false,
      row: {},
      current: 1,
    };
  },
  async created() {
    //获取页面初始数据
    this.init();
  },
  methods: {
    //初始化数据
    async init(page = 1, size = 10) {
      const result = await getDataByPageAndSize(page, size);
      // console.log(result);
      if (result.msg) {
        this.tableData = result.data.slice();
        this.total = result.count;
        this.current = page;
      }
    },

    // 删除 按钮回调
    async del(e) {
      console.log(e.id);
      const result = await deleteById(e.id);
      console.log(result);
      if (result.msg) {
        this.init();
      }
    },
    // 添加 或 编辑 弹窗的确定按钮
    async confirm(data) {
      this.closeDialog();
      const result = await this.confirmAjax(data);
      console.log(result);
      if (result.msg) {
        this.init();
      }
    },
    //发起添加 或 编辑 的请求
    async confirmAjax(data) {
      let edit = data.edit;
      let formData = data;

      this.$delete(data, "edit");

      // data.status === "0" ? (data.status = 0) : (data.status = 1);
      data.status = data.status === "0" ? 0 : 1;

      console.log(data);
      if (typeof data.file != "string") {
        //生成数据
        formData = new FormData();
        Object.keys(data).forEach((item) => formData.append(item, data[item]));
      }

      if (edit) return editData(formData);
      return appendData(formData);
    },

    //发起 搜索 的请求
    async onSearch(name) {
      const result = await LikeSearch(name, 1, 10);
      console.log(result);
      if (!result.msg) return;
      this.tableData = result.data.slice();
      this.total = result.count;
    },

    //审核按钮回调
    async checked(e) {
      this.closeDialog();
      await review(e);
      this.init();
    },
    //改变页码
    changePage(page) {
      this.init(page);
    },

    //显示全部 按钮回调
    showAll() {
      this.init();
    },

    //打开 添加 弹窗
    showModel() {
      this.isShow = true;
      this.row = {};
    },

    //打开 审核 弹窗
    handleCheck(e) {
      this.checking = true;
      console.log(e);
      this.row = { ...e, checking: true };
    },

    //打开 编辑 弹窗
    edit(e) {
      this.isShow = true;
      this.row = { ...e, edit: true };
      // console.log(this.row);
    },

    //弹唱的 取消 按钮
    cancel() {
      this.closeDialog();
    },

    //关闭弹窗
    closeDialog() {
      this.isShow = false;
      this.checking = false;
      this.showing = false;
    },

    other(e) {
      this.showing = true;
      this.row = { ...e, edit: true };
      console.log("审核");
    },

    async CardConfirm(e) {
      this.closeDialog();
      this.$notification["success"]({
        message: "已经更新啦！",
        description: "已更新所有消息",
      });
      //生成数据
      const res = await Check(e.id, e.check);
      console.log(res);
      if (res.msg) {
        await this.init();
      }
    },
    async CardCancel(e) {
      console.log(e);
      this.closeDialog();
      this.$notification["success"]({
        message: "已经更新啦！",
        description: "已更新所有消息",
      });
      //生成数据
      const res = await Check(e.id, e.check);
      console.log(res);
      if (res.msg) {
        await this.init();
      }
    },
  },
};
</script>

<style lang="less" scoped>
.Inventory {
  height: 100%;
  display: flex;
  flex-direction: column;
  .InventoryTable {
    flex: 1;
  }
}
</style>